package com.xingdata.jjxc.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.xingdata.jjxc.R;
import com.xingdata.jjxc.enty.ScaleEntity;
import com.xingdata.jjxc.utils.ViewTools;

/**
 * 
* @Description: TODO(自定义折线图)    
* @date 2015-10-14 上午11:04:22    
* Creater JiangMin
 */
public class ChartView extends View {

	public ChartView(Context context, AttributeSet attrs) {

		super(context, attrs);

	}

	// X轴长度
	private float xLengh = ViewTools.getScreenWidth() - 100;
	// // Y轴长度
	private float yLengh = (ViewTools.getScreenHight() - 400) / 2;
	// 起点X
	private float XStart = 80;
	// 起点Y
	private float YStart = (ViewTools.getScreenHight() - 380) / 2;

	// X轴单位
	// private float nXScale;
	// // Y轴单位
	private float nYScale;
	private ScaleEntity scaleEntity;
	private Context context;

	/**
	 * shadePaint 阴影画笔 -- brokenlinePaint折线画笔
	 */
	private Paint shadePaint, linePaint, circlePaint,titlePaint,textPaint;
	public void setViewInfo(ScaleEntity scaleEntity, Context context) {
		this.scaleEntity = scaleEntity;
		this.context = context;
		nYScale = (scaleEntity.yLengh / 6) / scaleEntity.nYScale;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		linePaint = new Paint();
		linePaint.setStyle(Paint.Style.FILL_AND_STROKE);// 设置填充样式
		linePaint.setAntiAlias(true);// 去锯齿
		linePaint.setColor(getResources().getColor(R.color.LightGray));// 颜色
		
		titlePaint= new Paint();
		titlePaint.setTextSize(28);
		titlePaint.setColor(getResources().getColor(R.color.white));// 颜色
		
		textPaint= new Paint();
		textPaint.setColor(getResources().getColor(R.color.white));// 颜色
		textPaint.setTextSize(23);
		
		shadePaint = new Paint();
		shadePaint.setStyle(Paint.Style.FILL_AND_STROKE);// 设置填充样式
		shadePaint.setAntiAlias(true);// 去锯齿
		shadePaint.setColor(getResources().getColor(R.color.LightGray));// 颜色
		shadePaint.setAlpha(80);
		


		circlePaint = new Paint();
		circlePaint.setStyle(Paint.Style.FILL);// 设置填充样式
		circlePaint.setAntiAlias(true);// 去锯齿
		circlePaint.setColor(getResources().getColor(R.color.circle_green));// 颜色
		circlePaint.setDither(true);
		// XY轴刻度
		drawXYscale(canvas, linePaint);
		// 画多边形阴影部分
		Path path = new Path();
		path.moveTo(XStart, YStart);

		for (int j = 0; j < scaleEntity.xScale.length; j++) {
			// 画圆形
			canvas.drawCircle(XStart + (j + 1) * scaleEntity.nXScale, YStart
					- scaleEntity.yScale[j] * nYScale, 6, circlePaint);
			// 画透明大圆
			canvas.drawCircle(XStart + (j + 1) * scaleEntity.nXScale, YStart
					- scaleEntity.yScale[j] * nYScale, 10, shadePaint);
			path.lineTo(XStart + (j + 1) * scaleEntity.nXScale, YStart
					- scaleEntity.yScale[j] * nYScale);

			if (j == scaleEntity.yScale.length - 1) {
				path.lineTo(XStart + (j + 1) * scaleEntity.nXScale, YStart);
				path.lineTo(XStart, YStart);
			}
		}

		canvas.drawPath(path, shadePaint);
	}

	/**
	 * 画XY轴
	 * 
	 * @param canvas
	 * @param paint
	 */
	private void drawXYscale(Canvas canvas, Paint paint) {
		//Title
		canvas.drawText(scaleEntity.title, XStart-30, YStart-scaleEntity.yLengh-30, titlePaint);
		// X轴
		canvas.drawLine(XStart, YStart, XStart + xLengh, YStart, linePaint);
		// Y轴
		canvas.drawLine(XStart, YStart, XStart, YStart - scaleEntity.yLengh,
				linePaint);
		for (int i = 0; i < scaleEntity.xScale.length; i++) {
			//画X轴线段
			canvas.drawLine(XStart + (i + 1) * scaleEntity.nXScale, YStart,
					XStart + (i + 1) * scaleEntity.nXScale, YStart
							- scaleEntity.yLengh, paint);
			//画X轴数字
			canvas.drawText(scaleEntity.xScale[i] + "", XStart + (i + 1)
					* scaleEntity.nXScale - 15, YStart + 30, textPaint);
		}
		
		for (int i = 0; i < 5; i++) {
			// 画Y轴线段
			canvas.drawLine(XStart, YStart - (i + 1) * nYScale
					* scaleEntity.nYScale, XStart + 20, YStart - (i + 1)
					* nYScale * scaleEntity.nYScale, paint);
			// 画Y轴数字
			canvas.drawText(Math.round((i + 1) * scaleEntity.nYScale) + "",
					XStart - 50, YStart - (i + 1) * nYScale
							* scaleEntity.nYScale + 8, textPaint);
		}
	}


	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(ViewTools.getScreenWidth() - 10,
				Math.round(yLengh + 50));
	}
}
